<?php
	require_once("pre.php");
	require_once("auth.php");
	require_once("../classes/Pays.class.php");
	require_once("../classes/Paysdesc.class.php");
	require_once("../classes/Commande.class.php");
	require_once("../classes/Venteprod.class.php");
	require_once("../classes/Client.class.php");
	require_once("../classes/Venteadr.class.php");
	require_once("../classes/Modules.class.php");
	require_once("../classes/Modulesdesc.class.php");
	require_once("../fonctions/divers.php");
?>
<?php if(! est_autorise("acces_commandes")) exit; ?>
<?php
	if(isset($_POST["action"]) && $_POST["action"] == "ajouter"){
		$total = 0;
		$poids = 0;

		$modules = new Modules();
		$modules->charger_id($type_paiement);

		$client = new Client();
		$client->charger_ref($id_client);

		$nbart = $_SESSION["commande"]->nbart;
		$sessionventeprod = $_SESSION["commande"]->venteprod;

		$commande = new Commande();
		$commande->client = $client->id;
		$commande->date = date("Y-m-d H:i:s");
		$commande->ref = "C" . date("ymdHis") . strtoupper(ereg_caracspec(substr($client->prenom,0, 3)));
		$commande->livraison = "L" . date("ymdHis") . strtoupper(ereg_caracspec(substr($client->prenom,0, 3)));
		$commande->transaction = date("His");
		$commande->transport = $type_livraison;
		$commande->paiement = $type_paiement;
		$commande->statut=1;

		$adr = new Venteadr();
		$adr->raison = $client->raison;
		$adr->nom = $client->nom;
		$adr->prenom = $client->prenom;
		$adr->adresse1 = $client->adresse1;
		$adr->adresse2 = $client->adresse2;
		$adr->adresse3 = $client->adresse3;
		$adr->cpostal = $client->cpostal;
		$adr->ville = $client->ville;
		$adr->tel = $client->telfixe . "  " . $client->telport;
		$adr->pays = $client->pays;
		$adrcli = $adr->add();
		$commande->adrfact = $adrcli;
		$commande->adrlivr = $adrcli;

		$commande->facture = 0;

		$idcmd = $commande->add();
		$commande->charger($idcmd);

		for($i=0;$i<$nbart;$i++){
			$produit = new Produit();
			$venteprod = new Venteprod();

			if($produit->charger($sessionventeprod[$i]->ref)){
				$produit->stock -=$sessionventeprod[$i]->quantite;
				$poids += $produit->poids;
				$produit->maj();
			}

			$venteprod->ref = $sessionventeprod[$i]->ref;
			$venteprod->titre = $sessionventeprod[$i]->titre;
			$venteprod->quantite = $sessionventeprod[$i]->quantite;
			$venteprod->tva = $sessionventeprod[$i]->tva;
			$venteprod->prixu = $sessionventeprod[$i]->prixu;
			$venteprod->commande = $idcmd;
			$venteprod->add();

			$total += $venteprod->prixu * $venteprod->quantite;

		}
		$commande->remise = 0;
		if($client->pourcentage>0) $commande->remise = $total * $client->pourcentage / 100;
		if($remise != "") $commande->remise+=$remise;

		$commande->port = $fraisport;

		$commande->maj();

		ActionsModules::instance()->appel_module("aprescommande", $commande);

		// Appeler la fonction mail() du module
		try {
			$module = ActionsModules::instance()->instancier($modules->nom);

			$module->mail($commande);
		} catch (Exception $e) { }

		header("location: commande_details.php?ref=".$commande->ref);

	}



	$_SESSION["commande"] = "";
	$_SESSION["commande"]->nbart = 0;
	$_SESSION["commande"]->venteprod = array();
	//$_SESSION["commande"]->commande = new Commande();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
	require_once("title.php");
?>
<script type="text/javascript" src="../lib/jquery/jquery.js"></script>
<script type="text/javascript" src="../lib/jquery/thickbox.js"></script>
<link rel="stylesheet" href="../lib/jquery/thickbox.css" type="text/css" media="screen" />



<script type="text/javascript">
function lookup(inputString) {
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
			$.get("listecli.php", {queryString: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
		}
	} // lookup



	function fill(str) {
		if (str != "") {
			var tableau = str.split("|");
			$('#inputstring').val(tableau[1]);
			$('#id_client').val(tableau[0]);
			setTimeout("$('#suggestions').hide();", 200);
		}
		else{
			$('#inputstring').val(str);
			setTimeout("$('#suggestions').hide();", 200);
		}
	}

function creercli(){
	if(document.getElementById("raison").value != "" &&
	document.getElementById("nom").value != "" &&
	document.getElementById("prenom").value != "" &&
	document.getElementById("adresse1").value != "" &&
	document.getElementById("cpostal").value != "" &&
	document.getElementById("ville").value != "" &&
	document.getElementById("email1").value != "" &&
	document.getElementById("email2").value != ""){

		if(document.getElementById("email1").value == document.getElementById("email2").value){
			args = "";
			args += "&raison=" + document.getElementById("raison").value;
			args += "&nom=" + document.getElementById("nom").value;
			args += "&prenom=" + document.getElementById("prenom").value;
			args += "&adresse1=" + document.getElementById("adresse1").value;
			args += "&cpostal=" + document.getElementById("cpostal").value;
			args += "&ville=" + document.getElementById("ville").value;
			args += "&email1=" + document.getElementById("email1").value;
			args += "&email2=" + document.getElementById("email2").value;
			args += "&entreprise=" + document.getElementById("entreprise").value;
			args += "&siret=" + document.getElementById("siret").value;
			args += "&intracom=" + document.getElementById("intracom").value;
			args += "&adresse2=" + document.getElementById("adresse2").value;
			args += "&adresse3=" + document.getElementById("adresse3").value;
			args += "&pays=" + document.getElementById("pays").value;
			args += "&telfixe=" + document.getElementById("telfixe").value;
			args += "&telport=" + document.getElementById("telport").value;
			args += "&remise=" + document.getElementById("remise").value;
			args += "&type=" + document.getElementById("type").value;
			//ajax
			$.ajax({
				type:'GET',
				url:"ajoutcli.php",
				data:'action=ajouter'+args,
				success:function(html){
					$('#nclient').html(html)
				}
			})
			tb_remove();
		}
		else{
			alert("vérifier le mail");
		}

	}
	else{
		alert("vérifier les champs obligatoires");

	}
}


function verifref(){
	var ref = document.getElementById("ref").value;
	$.ajax({
		type:'GET',
		url:"verifref.php",
		data:"ref="+ref,
		success:function(html){
			$('#titre').val(html.split("|")[0]);
			$('#prixu').val(html.split("|")[1]);
			$('#tva').val(html.split("|")[2]);
			document.getElementById("qtite").value="";
		}
	})
}

function addcom(){
	$.ajax({
		type:'GET',
		url:'addcom.php',
	data:"ref="+document.getElementById('ref').value+"&titre="+document.getElementById('titre').value+"&prixu="+document.getElementById('prixu').value+"&tva="+document.getElementById('tva').value+"&qtite="+document.getElementById('qtite').value,
		success:function(html){
			$('#listecom').html(html);
			document.getElementById("prod").reset();
		}
	})

}

function valid(){
	if(document.getElementById('id_client').value != "" && document.getElementById('port') != ""){
		document.getElementById('formulaire').submit();
	}
	else{
		alert("vérifier le client et les frais de port");
	}
}
</script>
</head>
<body>

<div id="wrapper">
<div id="subwrapper">

<?php
	$menu="commande";
	require_once("entete.php");
?>


<div id="contenu_int">
	<p><a href="accueil.php" class="lien04"><?php echo trad('Accueil'); ?> </a><img src="gfx/suivant.gif" width="12" height="9" border="0" /><a href="commande.php" class="lien04"><?php echo trad('Gestion_commandes'); ?></a>
    </p>
    <!-- Début de la colonne de gauche -->
<div id="bloc_description">
<div class="bordure_bottom">
<div class="entete_liste_client">
	<div class="titre"><?php echo trad('CREATION_COMMANDE'); ?></div>
	<div class="fonction_valider"><a href="#" onclick="valid()"><?php echo trad('VALIDER_LES_MODIFICATIONS'); ?></a></div>
</div>
<form action="commande_creer.php" method="post" id="formulaire">
	<input type="hidden" name="action" value="ajouter" />
<ul class="ligne_claire_BlocDescription" style="background-image: url(gfx/degrade_ligne1.png); background-repeat: repeat-x;">
		<li class="designation" style="width:280px; background-image: url(gfx/degrade_ligne1.png); background-repeat: repeat-x;"><?php echo trad('Choix_client'); ?> <span class="note"><?php echo trad('commencer'); ?></span></li>
		<li>
		<div id="nclient">
			<input type="hidden" name="id_client" id="id_client" value="" />
			<input name="choixdecli" id="inputstring" onkeyup="lookup(this.value);" type="text" class="form" size="40" />
		</div>
			</li>

		</ul>
		<div class="suggestionsBox" id="suggestions" style="display: none;">
			<img src="gfx/upArrow.png" style="float:left; margin:-10px 0 0 140px;" alt="upArrow" />
			<div class="suggestionList" id="autoSuggestionsList">
				&nbsp;
			</div>
		</div>


	<ul class="ligne_fonce_BlocDescription">
		<li class="designation" style="width:280px;"><?php echo trad('Ou_creer_client'); ?></li>
		<li>
				 <a href="#TB_inline?height=400&amp;width=800&amp;inlineId=contenu_cli&amp;modal=true" class="thickbox"><?php echo trad('creer_client'); ?></a>
				</li>
	</ul>
	<ul class="ligne_claire_BlocDescription">
		<li class="designation" style="width:280px;"><?php echo trad('Type_paiement'); ?></li>
		<li><select name="type_paiement" class="form_client">
					<option value=""><?php echo trad('Choisir'); ?>... </option>
				<?php
					$modules = new Modules();
					$query = "select * from $modules->table where type=1 and actif=1";
					$resul = mysql_query($query,$modules->link);
					while($row = mysql_fetch_object($resul)){
						$modulesdesc = new Modulesdesc();
						$modulesdesc->charger($row->nom, $_SESSION["util"]->lang);
						?>
						<option value="<?php echo $row->id; ?>"><?php echo $modulesdesc->titre; ?></option>
						<?php
					}
				?>
				</select></li>
	</ul>
	<ul class="ligne_fonce_BlocDescription">
		<li class="designation" style="width:280px;"><?php echo trad('Type_transport'); ?></li>
		<li><select name="type_livraison" class="form_client">
					<option value=""><?php echo trad('Choisir'); ?>... </option>
				<?php
					$modules = new Modules();
					$query = "select * from $modules->table where type=2 and actif=1";
					$resul = CacheBase::getCache()->mysql_query($query, $modules->link);

					foreach($resul as $row) {
						$modulesdesc = new Modulesdesc();
						$modulesdesc->charger($row->nom, $_SESSION["util"]->lang);
						?>
						<option value="<?php echo $row->id; ?>"><?php echo $modulesdesc->titre; ?></option>
						<?php
					}
				?>
				</select>
</li>
	</ul>
	<ul class="ligne_claire_BlocDescription">
		<li class="designation" style="width:280px;"><?php echo trad('Montant_frais_port'); ?></li>
		<li><input type="text" name="fraisport" class="form" size="40"></li>
	</ul>
	<ul class="ligne_fonce_BlocDescription">
		<li class="designation" style="width:280px;"><?php echo trad('Remise'); ?></li>
		<li><input type="text" name="remise" class="form" size="40" /></li>
	</ul>

</form>
</div>
<div class="bordure_bottom" style="margin:10px 0 0px 0;">
<div class="entete_liste_client">
	<div class="titre"><?php echo trad('AJOUT_PRODUITS'); ?></div>
	<div class="fonction_valider"><a href="#" onclick="addcom()"><?php echo trad('AJOUTER_PANIER'); ?></a></div>
</div>
<form id="prod">
	<ul class="ligne_claire_BlocDescription">
		<li class="designation" style="width:280px;"><?php echo trad('Ref_produit'); ?></li>
		<li><input type="text" name="ref" id="ref" value="" class="form" size="40" onblur="verifref()" /></li>
	</ul>
	<ul class="ligne_fonce_BlocDescription">
		<li class="designation" style="width:280px;"><?php echo trad('Titre_produit'); ?></li>
		<li><input type="text" name="titre" id="titre" value="" class="form" size="40" /></li>
	</ul>
	<ul class="ligne_claire_BlocDescription">
		<li class="designation" style="width:280px;"><?php echo trad('Prix_unitaire'); ?></li>
		<li><input type="text" name="prixu" id="prixu" value="" class="form" size="40" /></li>
	</ul>
	<ul class="ligne_fonce_BlocDescription">
		<li class="designation" style="width:280px;"><?php echo trad('TVA_applicable_sur_produit'); ?></li>
		<li><input type="text" name="tva" id="tva" value="" class="form" size="40" /></li>
	</ul>
	<ul class="ligne_claire_BlocDescription">
		<li class="designation" style="width:280px;"><?php echo trad('Quantite'); ?></li>
		<li><input type="text" name="qtite" id="qtite" value="" class="form" size="40" /></li>
	</ul>
</form>
</div>

	<div id="contenu_cli" style="display:none">
	<div id="bloc_description">
	<div class="entete_liste_client">
			<div class="titre"><?php echo trad('CREATION_CLIENT'); ?></div>
			<div class="fonction_valider"><a href="#" onclick="creercli()"><?php echo trad('VALIDER_LES_MODIFICATIONS'); ?></a></div>
		</div>
		<table width="100%" cellpadding="5" cellspacing="0">
    <tr class="claire">
        	<th class="designation" width="290"><?php echo trad('Societe'); ?></th>
		    <th><input name="entreprise" id="entreprise" type="text" class="form" size="40" <?php if(isset($entreprise)) echo "value=\"".htmlspecialchars($entreprise)."\""; ?> /></th>
	</tr>
	<tr class="fonce">
			<td class="designation"><?php echo trad('Siret'); ?></td>
		    <td><input name="siret" id="siret" type="text" class="form" size="40" <?php if(isset($siret)) echo "value=\"".htmlspecialchars($siret)."\""; ?> /></td>
	</tr>
	<tr class="claire">
		    <td class="designation"><?php echo trad('Numintracom'); ?></td>
		    <td><input name="intracom" id="intracom" type="text" class="form" size="40" <?php if(isset($intracom)) echo "value=\"".htmlspecialchars($intracom)."\""; ?> /></td></tr>
	<tr class="fonce">
			<td class="designation">Civilité <?php if($erreurraison) echo trad('Obligatoire'); ?></td>
			<td><input name="raison" type="radio" id="raison" class="form" value="1" <?php if(isset($raison) && $raison == 1) echo "checked"; ?>/>
		<?php echo trad('Mme'); ?>
		<input name="raison" type="radio" class="form" value="2" <?php if(isset($raison) && $raison == 2) echo "checked"; ?>/>
		<?php echo trad('Mlle'); ?>
		<input name="raison" type="radio" class="form" value="3" <?php if(isset($raison) && $raison == 3) echo "checked"; ?>/>
		<?php echo trad('M.'); ?></td>
	</tr>
	<tr class="claire">
		   	<td class="designation"><?php echo trad('Nom'); ?> <?php if($erreurnom) echo trad('Obligatoire');  ?></td>
		    <td><input name="nom" id="nom" type="text" class="form" size="40" <?php if(isset($nom)) echo "value=\"".htmlspecialchars($nom)."\""; ?> /></td>
	</tr>
	<tr class="fonce">
		    <td class="designation"><?php echo trad('Prenom'); ?> <?php if($erreurprenom) echo trad('Obligatoire'); ?></td>
		    <td><input name="prenom" id="prenom" type="text" class="form" size="40" <?php if(isset($prenom)) echo "value=\"".htmlspecialchars($prenom)."\""; ?> /></td>
	</tr>
	<tr class="claire">
		    <td class="designation"><?php echo trad('Adresse'); ?> <?php if($erreuradresse) echo trad('Obligatoire'); ?></td>
		    <td><input name="adresse1" id="adresse1" type="text" class="form" size="40" <?php if(isset($adresse1)) echo "value=\"".htmlspecialchars($adresse1)."\""; ?>/></td>
	</tr>
	<tr class="fonce">
		    <td class="designation"><?php echo trad('Adressesuite'); ?></td>
		    <td><input name="adresse2" id="adresse2" type="text" class="form" size="40" <?php if(isset($adresse2)) echo "value=\"".htmlspecialchars($adresse2)."\""; ?>/></td>
	</tr>
	<tr class="claire">
		    <td class="designation"><?php echo trad('Adressesuite'); ?> 2</td>
		    <td><input name="adresse3" id="adresse3" type="text" class="form" size="40" <?php if(isset($adresse3)) echo "value=\"".htmlspecialchars($adresse3)."\""; ?>/></td>
	</tr>
	<tr class="fonce">
		    <td class="designation"><?php echo trad('CP'); ?> <?php if($erreurcpostal) echo trad('Obligatoire'); ?></td>
		    <td><input name="cpostal" id="cpostal" type="text" class="form" size="40" <?php if(isset($cpostal)) echo "value=\"".htmlspecialchars($cpostal)."\""; ?>/></td>
	</tr>
	<tr class="claire">
			<td class="designation"><?php echo trad('Ville'); ?> <?php if($erreurville) echo trad('Obligatoire'); ?></td>
			<td><input name="ville" id="ville" type="text" class="form" size="40" <?php if(isset($ville)) echo "value=\"".htmlspecialchars($ville)."\""; ?>/></td>
	</tr>
	<tr class="fonce">
		    <td class="designation">Pays <?php if($erreurpays) echo trad('Obligatoire'); ?></td>
		    <td><select name="pays" id="pays" class="form_client">
		     <?php
		      	$pays = new Pays();
		      	$query ="select * from $pays->table";

		      	$resul = mysql_query($query, $pays->link);
		      	while($row = mysql_fetch_object($resul)){
					$paysdesc = new Paysdesc();
					if ($paysdesc->charger($row->id, $_SESSION["util"]->lang)) {

		      ?>
		      <option value="<?php echo $row->id; ?>" <?php if($paysform == $row->id || $row->defaut){ echo "selected"; } ?> ><?php echo($paysdesc->titre); ?></option>
		      <?php }
			  }?>
		      </select>
			</td>
	</tr>
	<tr class="claire">
		    <td class="designation"><?php echo trad('Telfixe'); ?></td>
		    <td><input name="telfixe" id="telfixe" type="text" class="form" size="40" <?php if(isset($telfixe)) echo "value=\"".htmlspecialchars($telfixe)."\""; ?>/></td>
	</tr>
	<tr class="fonce">
		    <td class="designation"><?php echo trad('Telport'); ?></td>
		    <td><input name="telport" id="telport" type="text" class="form" size="40" <?php if(isset($telport)) echo "value=\"".htmlspecialchars($telport)."\""; ?>/></td>
	</tr>
	<tr class="claire">
		    <td class="designation"><?php echo trad('E-mail'); ?> <?php if($erreurmail) echo trad('Obligatoire'); else if($erreurmailexiste) echo "existe déjà"; ?></td>
		    <td><input name="email1" id="email1" type="text" class="form" size="40" /></td>
	</tr>
	<tr class="fonce">
		    <td class="designation"><?php echo trad('Confirme-mail'); ?></td>
		    <td><input name="email2" id="email2" type="text" class="form" size="40" /></td>
	</tr>
	<tr class="claire">
		    <td class="designation"><?php echo trad('Remise'); ?> </td>
		    <td><input name="pourcentage" id="remise" type="text" class="form" size="40" <?php if(isset($remise)) echo "value=\"".htmlspecialchars($remise)."\""; ?>/></td>
	</tr>
	<tr class="foncebottom">
		    <td class="designation"><?php echo trad('Revendeur'); ?> </td>
		    <td><input type="checkbox" name="type" id="type" class="form" <?php if(isset($type)) echo "checked"; ?>/></td>
	</tr>
</table>
		<input type="button" value="annuler" onclick="tb_remove()" />
	</div>
	</div>
</div>
<!-- fin du bloc description -->
<!-- bloc colonne de droite -->
<div id="bloc_colonne_droite">
		<div id="listecom"> </div>
</div>
</div>
<?php require_once("pied.php");?>
</div>
</div>
</body>
</html>
